﻿using Models.Common;
using Models.Device;
using UtilsBase;
using GatewayServerService.CloudCenter;

namespace GatewayServerService.Device
{
    /// <summary>
    /// 普锐森社 RS485光照温湿三合一(pr - 3002 - gzws - n01)
    /// </summary>
    public class ServiceDeviceRS4851013_GWS
    {
        ServiceCloudDeviceValueLog log = new ServiceCloudDeviceValueLog();
        //回调
        public void callBackUpdate(MQReqModel<object> httpReqModel)
        {
            try
            {
                ModelReceverData modelDeviceData = httpReqModel.toServerData;
                CommandLogModel commandLogModel = httpReqModel.toGatewayData;

                if (string.IsNullOrWhiteSpace(modelDeviceData.commandResults[0].value))
                {
                    return;
                }


                //ModelCommandAgreement modelCommandAgreement = JsonConvert.DeserializeObject<ModelCommandAgreement>(commandLogModel.parameter);
                //ModelCommandAgreementParameter modelCommandAgreementParameter = UtilsObject.DynamicToEntity<ModelCommandAgreementParameter>(modelCommandAgreement.agreementParameter);
                //List<ModelCommandAgreementParameterCommand> commands = UtilsObject.DynamicToEntity<List<ModelCommandAgreementParameterCommand>>(modelCommandAgreement.agreementParameter.command);
                //ModelCommandAgreementParameterCommand command = commands.Where(c => c.key == commandLogModel.key).FirstOrDefault();
                //普锐森社
                //RS485光照温湿三合一(pr - 3002 - gzws - n01)
                //1）产品为0~65535量程变送器，单位为1Lux
                //0530 H(十六进制) = 1328=> 光照度 = 1328 Lux
                //2）产品为0~200000量程变送器，单位为百Lux
                //光照

                //温度：当温度低于0℃时以补码形式上传
                //FF9B H(十六进制) = -101 => 温度 = -10.1℃
                //湿度：
                //292 H(十六进制) = 658=> 湿度 = 65.8 % RH
                string result = modelDeviceData.commandResults[0].value;

                if (commandLogModel.key == "temperatureANDhumidity")
                {
                    //解析
                    string humidityHex = result.Substring(9, 5);
                    string temperatureHex = result.Substring(15, 5);
                    double temperature = 0.0;
                    double humidity = 0.0;
                    string temperatureBinary = UtilsDataTypeChange.hexToBinaryTolStr(temperatureHex);
                    humidity = new UtilsDataTypeChange().hex16Convert10(humidityHex) * 0.1;

                    if (temperatureBinary.Substring(0, 1) == "0")
                    {
                        temperature = new UtilsDataTypeChange().hex16Convert10(temperatureHex) * 0.1;
                    }
                    else if (temperatureBinary.Substring(0, 1) == "1")
                    {
                        string bumabinary = UtilsByte.GetBuMaBinary(temperatureBinary);
                        temperature = UtilsDataTypeChange.binaryToDecimalStr(bumabinary) * -0.1;
                    }

                    Console.WriteLine("humidity:" + humidity + "%RH");
                    Console.WriteLine("temperature:" + temperature + "℃");

                    ServiceCloudDeviceValueLog log = new ServiceCloudDeviceValueLog();
                    //写日志
                    log.writeSensorlog(commandLogModel.d_guid, "temperature", UtilsNumber.GetNumberForLength(temperature, 1).ToString(), "温度");
                    log.writeSensorlog(commandLogModel.d_guid, "humidity", UtilsNumber.GetNumberForLength(humidity, 1).ToString(), "湿度");
                }
                if (commandLogModel.key == "light")
                {
                    string lightHex = result.Substring(9, 5);
                    double light = new UtilsDataTypeChange().hex16Convert10(lightHex);
                    Console.WriteLine(light + " lur");
                    ServiceCloudDeviceValueLog log = new ServiceCloudDeviceValueLog();
                    log.writeSensorlog(commandLogModel.d_guid, "light", UtilsNumber.GetNumberForLength(light, 1).ToString(), "光强度");
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

    }

}
